package com.infodms.dms.actions.sales.marketmanage.planissued;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.sales.marketmanage.planissued.ActivitiesPlanIssuedDao;
import com.infodms.dms.dao.sales.planmanage.SpacePlanManageDao;
import com.infodms.dms.dao.sales.planmanage.SpacePlanReportDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TtCampaignExecutePO;
import com.infodms.dms.util.CommonUtils;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

/**
 * @Title: 活动信息查询Action
 * @Description:InfoFrame3.0.V01
 * @Copyright: Copyright (c) 2010
 * @Company: www.infoservice.com.cn
 * @Date: 2010-7-6
 * @author 
 * @mail  
 * @version 1.0
 * @remark 
 */
public class ActivitiesPlanAreaQuery {
	
	public Logger logger = Logger.getLogger(ActivitiesPlanAreaQuery.class);   
	ActivitiesPlanIssuedDao dao  = ActivitiesPlanIssuedDao.getInstance();
	private ActionContext act = ActionContext.getContext();
	RequestWrapper request = act.getRequest();
	private final String initUrl = "/jsp/sales/marketmanage/planissued/activitiesPlanAreaQuery.jsp";
	private final String detailUrl = "/jsp/sales/marketmanage/planissued/activitiesPlanQueryAreaDetail.jsp";
	/**
	 * 活动信息查询页面初始化
	 */
	public void activitiesPlanAreaQueryInit(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			String orgId = logonUser.getOrgId().toString();
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
			
			act.setOutData("areaList", areaList);
			act.setOutData("orgId", orgId);
			act.setForword(initUrl);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动信息查询页面初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 活动信息查询
	 */
	public void activitiesPlanAreaQuery(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			String campaignNo = request.getParamValue("campaignNo");			//方案编号
			String campaignName = request.getParamValue("campaignName");		//方案名称
			String campaignSubject = request.getParamValue("campaignSubject");	//活动主题
			String campaignType = request.getParamValue("campaignType");		//活动类别
			String startDate = request.getParamValue("startDate");				//开始时间
			String endDate = request.getParamValue("endDate");					//结束时间
			String checkStatus =request.getParamValue("checkStatus");			//方案状态
			String dealerCode = request.getParamValue("dealerCode");			//经销商CODE
			String areaId = CommonUtils.checkNull(request.getParamValue("areaId")) ;
			
			if (CommonUtils.isNullString(areaId)) {
				if(MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).size()>0){
						for(int i=0;i<MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).size();i++){
							if((i+1)!=MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).size()){
								areaId=MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).get(i).get("AREA_ID").toString()+","+areaId;
							}else{
								areaId=areaId+MaterialGroupManagerDao.getPoseIdBusiness(String.valueOf(logonUser.getPoseId())).get(i).get("AREA_ID").toString();
							}
						}
				}
			}
			
			String orgId = logonUser.getOrgId().toString();
			String dealerIds=logonUser.getDealerId();
			Integer curPage = request.getParamValue("curPage") !=null ? Integer.parseInt(request.getParamValue("curPage")) : 1;
			PageResult<Map<String, Object>> ps = dao.activitiesAreaQuery(areaId,dealerIds,orgId,campaignNo, campaignName, campaignSubject, campaignType, startDate, endDate, checkStatus, dealerCode, orgId,curPage, Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动信息查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 活动信息明细查询
	 */
	public void activitiesPlanAreaQueryDetail(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			String campaignId = request.getParamValue("campaignId");
			//String executeId = request.getParamValue("executeId");
			String orgId=String.valueOf(logonUser.getOrgId());
			TtCampaignExecutePO Mypo= new TtCampaignExecutePO();
			Mypo.setCampaignId(Long.parseLong(campaignId));
			Map<String, Object> map2;
			String dealerId=request.getParamValue("dealerId");
			String camType = CommonUtils.checkNull(request.getParamValue("camType")) ;
			
			String executeId="";
			if(Constant.CAMPAIGN_TYPE_03.toString().equals(camType)){
				executeId=String.valueOf( dao.queryMyExecute(orgId, campaignId).get(0).get("SPACE_ID"));
				map2 = dao.activitiesMyInfoQuery(campaignId, executeId,dealerId);
				
				List<Map<String, Object>> executePlans = SpacePlanManageDao.getInstance().getMyExecutePlanByCmpId(campaignId);
				
				act.setOutData("executePlans", executePlans) ;
			}else{
				executeId=String.valueOf( dao.queryMyCamExecute(dealerId, campaignId).get(0).get("EXECUTE_ID"));
				map2 = dao.activitiesMyInfoQuery(campaignId, executeId,dealerId);
			}
			//map2 = dao.activitiesInfoQuery(campaignId, executeId,orgId);
			String modelName = dao.getModelName(campaignId);
			map2.put("PROD_NAME", modelName);
			Map<String, Object> map3 = dao.activitiesTargetQuery(executeId);
			List<Map<String, Object>> list1 = dao.getActivitiesCostbyId(executeId, campaignId);
			List<Map<String, Object>> list2 = SpacePlanReportDao.getActivitiesMediaCost(executeId);
			List<Map<String, Object>> list3 = dao.getCheckInfo(executeId);
			List<Map<String, Object>> list4 = dao.getAttachInfo(executeId);
			List<Map<String, Object>> list5 = dao.getZJCheckInfo(executeId);
			
			String type = "" ;
			if(list1.size()>0&&list1.get(0).get("ITEM_COUNT")!=null){
				type = list1.get(0).get("COST_TYPE").toString() ;
			}
			if(list2.size()>0&&list2.get(0).get("ITEM_COUNT")!=null){
				type = list2.get(0).get("COST_TYPE").toString() ;
			}
			
			Map<String,Integer> mapC = new HashMap<String, Integer> () ;
			mapC.put("cy03", Constant.CAMPAIGN_TYPE_03) ;
			
			act.setOutData("type", type) ;
						
			act.setOutData("camType", camType) ;
			act.setOutData("mapC", mapC) ;
			
			act.setOutData("list5", list5);
			act.setOutData("list1", list1);
			act.setOutData("list2", list2);
			act.setOutData("list3", list3);
			act.setOutData("list4", list4);
			act.setOutData("map2", map2);
			act.setOutData("map3", map3);
			act.setForword(detailUrl);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动信息明细查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
}
